package com.dianping.cat.consumer.cross.model.transform;

import com.dianping.cat.consumer.cross.model.entity.CrossReport;
import com.dianping.cat.consumer.cross.model.entity.Local;
import com.dianping.cat.consumer.cross.model.entity.Name;
import com.dianping.cat.consumer.cross.model.entity.Remote;
import com.dianping.cat.consumer.cross.model.entity.Type;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;











public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public CrossReport buildCrossReport(Attributes attributes) {
    String domain = attributes.getValue("domain");
/*  30 */     String startTime = attributes.getValue("startTime");
    String endTime = attributes.getValue("endTime");
    CrossReport crossReport = new CrossReport(domain);

/*  34 */     if (startTime != null) {
/*  35 */       crossReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  38 */     if (endTime != null) {
/*  39 */       crossReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  42 */     return crossReport;
  }



/*  47 */   public String buildDomain(Attributes attributes) { throw new UnsupportedOperationException(); }




/*  52 */   public String buildIp(Attributes attributes) { throw new UnsupportedOperationException(); }



  public Local buildLocal(Attributes attributes) {
/*  57 */     String id = attributes.getValue("id");
/*  58 */     return new Local(id);
  }




  public Name buildName(Attributes attributes) {
/*  65 */     String id = attributes.getValue("id");
/*  66 */     String totalCount = attributes.getValue("totalCount");
/*  67 */     String failCount = attributes.getValue("failCount");
/*  68 */     String failPercent = attributes.getValue("failPercent");
/*  69 */     String avg = attributes.getValue("avg");
/*  70 */     String sum = attributes.getValue("sum");
/*  71 */     String tps = attributes.getValue("tps");
/*  72 */     Name name = new Name(id);

/*  74 */     if (totalCount != null) {
/*  75 */       name.setTotalCount(((Long)convert(Long.class, totalCount, Long.valueOf(0L))).longValue());
    }

    if (failCount != null) {
/*  79 */       name.setFailCount(((Integer)convert(Integer.class, failCount, Integer.valueOf(0))).intValue());
    }

/*  82 */     if (failPercent != null) {
/*  83 */       name.setFailPercent(toNumber(failPercent, "0.00", Integer.valueOf(0)).doubleValue());
    }

/*  86 */     if (avg != null) {
/*  87 */       name.setAvg(toNumber(avg, "0.00", Integer.valueOf(0)).doubleValue());
    }

/*  90 */     if (sum != null) {
/*  91 */       name.setSum(toNumber(sum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/*  94 */     if (tps != null) {
/*  95 */       name.setTps(toNumber(tps, "0.00", Integer.valueOf(0)).doubleValue());
    }

/*  98 */     return name;
  }


  public Remote buildRemote(Attributes attributes) {
/* 103 */     String id = attributes.getValue("id");
/* 104 */     String role = attributes.getValue("role");
/* 105 */     String app = attributes.getValue("app");
/* 106 */     String ip = attributes.getValue("ip");
/* 107 */     Remote remote = new Remote(id);

/* 109 */     if (role != null) {
/* 110 */       remote.setRole(role);
    }

/* 113 */     if (app != null) {
/* 114 */       remote.setApp(app);
    }

/* 117 */     if (ip != null) {
/* 118 */       remote.setIp(ip);
    }

/* 121 */     return remote;
  }


  public Type buildType(Attributes attributes) {
/* 126 */     String id = attributes.getValue("id");
/* 127 */     String totalCount = attributes.getValue("totalCount");
/* 128 */     String failCount = attributes.getValue("failCount");
/* 129 */     String failPercent = attributes.getValue("failPercent");
/* 130 */     String avg = attributes.getValue("avg");
/* 131 */     String sum = attributes.getValue("sum");
/* 132 */     String tps = attributes.getValue("tps");
/* 133 */     Type type = new Type();

/* 135 */     if (id != null) {
/* 136 */       type.setId(id);
    }

/* 139 */     if (totalCount != null) {
/* 140 */       type.setTotalCount(((Long)convert(Long.class, totalCount, Long.valueOf(0L))).longValue());
    }

/* 143 */     if (failCount != null) {
/* 144 */       type.setFailCount(((Integer)convert(Integer.class, failCount, Integer.valueOf(0))).intValue());
    }

/* 147 */     if (failPercent != null) {
/* 148 */       type.setFailPercent(toNumber(failPercent, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 151 */     if (avg != null) {
/* 152 */       type.setAvg(toNumber(avg, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 155 */     if (sum != null) {
/* 156 */       type.setSum(toNumber(sum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 159 */     if (tps != null) {
/* 160 */       type.setTps(toNumber(tps, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 163 */     return type;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 168 */     if (value == null || value.length() == 0) {
/* 169 */       return defaultValue;
    }

/* 172 */     if (type == Boolean.class || type == boolean.class)
/* 173 */       return (T)Boolean.valueOf(value); 
/* 174 */     if (type == Integer.class || type == int.class)
/* 175 */       return (T)Integer.valueOf(value); 
/* 176 */     if (type == Long.class || type == long.class)
/* 177 */       return (T)Long.valueOf(value); 
/* 178 */     if (type == Short.class || type == short.class)
/* 179 */       return (T)Short.valueOf(value); 
/* 180 */     if (type == Float.class || type == float.class)
/* 181 */       return (T)Float.valueOf(value); 
/* 182 */     if (type == Double.class || type == double.class)
/* 183 */       return (T)Double.valueOf(value); 
/* 184 */     if (type == Byte.class || type == byte.class)
/* 185 */       return (T)Byte.valueOf(value); 
/* 186 */     if (type == Character.class || type == char.class) {
/* 187 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 189 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 194 */     if (str == null || str.length() == 0) {
/* 195 */       return defaultValue;
    }

    try {
/* 199 */       return (new SimpleDateFormat(format)).parse(str);
/* 200 */     } catch (ParseException e) {
/* 201 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format, Number defaultValue) {
/* 206 */     if (str == null || str.length() == 0) {
/* 207 */       return defaultValue;
    }

    try {
/* 211 */       return (new DecimalFormat(format)).parse(str);
/* 212 */     } catch (ParseException e) {
/* 213 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/cross/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */